<?php
namespace yunj;

/**
 * 内部配置
 * Class Config
 * @package yunj
 */
final class Config{

    /**
     * Notes: 获取
     * Author: Uncle-L
     * Date: 2020/11/3
     * Time: 14:55
     * @param $key
     * @param null $def
     * @return mixed|null
     */
    public static function get($key,$def=null){
        static $options;
        $options=$options?:[];
        // fileName、keyArr
        if(!strstr($key,'.')){
            $fileName=$key;
            $keyArr=[];
        }else{
            $keyArr=explode('.',$key);
            $fileName=array_shift($keyArr);
        }

        // fileOptions
        if(!isset($options[$fileName])){
            $file=YUNJ_PATH.'library/config/'.$fileName.'.php';
            if(!file_exists($file)) return $def;
            $options[$fileName]=include($file);
        }
        $fileOptions=$options[$fileName];
        
        // return
        if(!$keyArr) return $fileOptions;
        $keyCount=count($keyArr);
        switch ($keyCount){
            case 1:
                list($k)=$keyArr;
                if(isset($fileOptions[$k])) $res=$fileOptions[$k];
                break;
            case 2:
                list($k1,$k2)=$keyArr;
                if(isset($fileOptions[$k1])&&is_array($fileOptions[$k1])&&isset($fileOptions[$k1][$k2]))
                    $res=$fileOptions[$k1][$k2];
                break;
        }
        return isset($res)?$res:$def;
    }

}